package u1project.client;

import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainClient {
    public static void main(String[] args) {
        Properties p=new Properties();
        //p.load(new FileReader(""));

        try {
            p.load(MainClient.class.getClassLoader().getResourceAsStream("u1project/ip.properties"));
            String ip=p.getProperty("ip");
            int port=Integer.parseInt(p.getProperty("port"));
            //xml > properties
            Socket s = new Socket(ip, port);
            //Socket s = new Socket("localhost", 9998);
            OutputStream outputStream = s.getOutputStream();
            InputStream inputStream = s.getInputStream();

            ObjectOutputStream oout=new ObjectOutputStream(outputStream);

            ObjectInputStream oin=new ObjectInputStream(inputStream);

            Request request = null;
            Response response = null;
            Scanner in=new Scanner(System.in);
            int choice=0;
            while (true){
                System.out.println("1:登录，2：注册，3：上传，4：下载，8：结账,9退出");
                choice=in.nextInt();
                if(choice==1){
                    System.out.println("----登录-----");
                    request=new Request("login");
                    System.out.println("请输入用户名：");
                    request.put("username",in.next());
                    System.out.println("请输入密码");
                    request.put("userpass",in.next());

                    oout.writeObject(request);
                    oout.flush();
                    oout.reset();//防止重发上次的对象
                    response= (Response)oin.readObject();
                    if (response.isSuccess()){
                        System.out.println("请求成功");
                    }else {
                        System.out.println("请求失败");
                    }

                }else if(choice==2){
                    System.out.println("---注册-----");
                    //...

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
